<?php

namespace App\V3\Api\Controllers;

use App\V3\Api\Models\SystemConfigModel;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Http\Request;

class SystemConfigController extends BaseController
{
    //
    public function set(Request $request)
    {
        if(in_array($request->val, [0, 1])) {
            SystemConfigModel::where('key_value', 'switch')->update(['value' => $request->val]);
            return $this->success();
        }
        else {
            return $this->fail('', '参数不合法');
        }
    }

    public function get()
    {
        $result = SystemConfigModel::where('key_value', 'switch')->first();
        return $this->success(
            $result['value']
        );
    }

    //设置体验版时长
    public function setExperience(Request $request)
    {
        if(!is_numeric($request->value)) {
            return $this->fail('', '输入的内容仅能为数字');
        }

        if(!is_integer($request->value * 1)) {
            return $this->fail('', '输入的内容仅能为整数');
        }
        if($request->value < 0) {
            return $this->fail('', '输入的内容不能小于0');
        }

        SystemConfigModel::where('key_value', 'experience_day')->update(['value' => $request->value]);
        return  $this->success();
    }

    public function getExperience()
    {
        $result = SystemConfigModel::where('key_value', 'experience_day')->first();
        return $this->success(
            $result['value']
        );
    }

    //是否开启系统维护
    public function isUpkeep()
    {
        $info = SystemConfigModel::where('key_value', 'is_upkeep')->first();

        if($info['value']) {
            $info->value = 0;
            $info->save();
            $tag = 0;
        }
        else {
            $info->value = 1;
            $info->save();
            $tag = 1;
        }

        return $this->success($tag);
    }

    //获取开启系统维护信息
    public function getIsUpkeep()
    {
        $info = SystemConfigModel::select('value')->where('key_value', 'is_upkeep')->first();
        return $this->success((int)$info['value']);
    }
}
